

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.feature.one_hot_encoder &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.feature.one_hot_encoder</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.feature.one_hot_encoder</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*-</span>
<span class="c1">#  Copyright 2019 The FATE Authors. All Rights Reserved.</span>
<span class="c1">#</span>
<span class="c1">#  Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1">#  you may not use this file except in compliance with the License.</span>
<span class="c1">#  You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1">#  Unless required by applicable law or agreed to in writing, software</span>
<span class="c1">#  distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1">#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1">#  See the License for the specific language governing permissions and</span>
<span class="c1">#  limitations under the License.</span>

<span class="kn">import</span> <span class="nn">functools</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">arch.api.proto</span> <span class="k">import</span> <span class="n">onehot_meta_pb2</span><span class="p">,</span> <span class="n">onehot_param_pb2</span>
<span class="kn">from</span> <span class="nn">arch.api.utils</span> <span class="k">import</span> <span class="n">log_utils</span>
<span class="kn">from</span> <span class="nn">federatedml.model_base</span> <span class="k">import</span> <span class="n">ModelBase</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic.data_overview</span> <span class="k">import</span> <span class="n">get_header</span>
<span class="kn">from</span> <span class="nn">federatedml.util</span> <span class="k">import</span> <span class="n">consts</span>
<span class="kn">from</span> <span class="nn">federatedml.param.onehot_encoder_param</span> <span class="k">import</span> <span class="n">OneHotEncoderParam</span>

<span class="n">LOGGER</span> <span class="o">=</span> <span class="n">log_utils</span><span class="o">.</span><span class="n">getLogger</span><span class="p">()</span>

<span class="n">MODEL_PARAM_NAME</span> <span class="o">=</span> <span class="s1">&#39;OneHotParam&#39;</span>
<span class="n">MODEL_META_NAME</span> <span class="o">=</span> <span class="s1">&#39;OneHotMeta&#39;</span>
<span class="n">MODEL_NAME</span> <span class="o">=</span> <span class="s1">&#39;OneHotEncoder&#39;</span>


<div class="viewcode-block" id="OneHotEncoder"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.one_hot_encoder.OneHotEncoder">[docs]</a><span class="k">class</span> <span class="nc">OneHotEncoder</span><span class="p">(</span><span class="n">ModelBase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">OneHotEncoder</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">schema</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">col_maps</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">output_data</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model_param</span> <span class="o">=</span> <span class="n">OneHotEncoderParam</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_init_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_param</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model_param</span> <span class="o">=</span> <span class="n">model_param</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols_index</span> <span class="o">=</span> <span class="n">model_param</span><span class="o">.</span><span class="n">cols</span>

<div class="viewcode-block" id="OneHotEncoder.fit"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.one_hot_encoder.OneHotEncoder.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init_cols</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>

        <span class="n">f1</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">record_new_header</span><span class="p">,</span>
                               <span class="n">cols</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">,</span>
                               <span class="n">header</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">)</span>

        <span class="n">col_maps</span> <span class="o">=</span> <span class="n">data_instances</span><span class="o">.</span><span class="n">mapPartitions</span><span class="p">(</span><span class="n">f1</span><span class="p">)</span>

        <span class="n">f2</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">merge_col_maps</span><span class="p">)</span>
        <span class="n">col_maps</span> <span class="o">=</span> <span class="n">col_maps</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">f2</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_detect_overflow</span><span class="p">(</span><span class="n">col_maps</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">col_maps</span> <span class="o">=</span> <span class="n">col_maps</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Before set_schema in fit, schema is : </span><span class="si">{}</span><span class="s2">, header: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">schema</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_schema</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="n">data_instances</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;After transform in fit, schema is : </span><span class="si">{}</span><span class="s2">, header: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">schema</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">data_instances</span></div>

    <span class="k">def</span> <span class="nf">_detect_overflow</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">col_maps</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">col_value_map</span> <span class="ow">in</span> <span class="n">col_maps</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">col_value_map</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">consts</span><span class="o">.</span><span class="n">ONE_HOT_LIMIT</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Input data should not have more than </span><span class="si">{}</span><span class="s2"> possible value when doing one-hot encode&quot;</span>
                                 <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">consts</span><span class="o">.</span><span class="n">ONE_HOT_LIMIT</span><span class="p">))</span>

<div class="viewcode-block" id="OneHotEncoder.transform"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.one_hot_encoder.OneHotEncoder.transform">[docs]</a>    <span class="k">def</span> <span class="nf">transform</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init_cols</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="n">ori_header</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_transform_schema</span><span class="p">()</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;In Onehot transform, ori_header: </span><span class="si">{}</span><span class="s2">, transfered_header: </span><span class="si">{}</span><span class="s2">, col_maps: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="n">ori_header</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">col_maps</span>
        <span class="p">))</span>
        <span class="n">one_data</span> <span class="o">=</span> <span class="n">data_instances</span><span class="o">.</span><span class="n">first</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">features</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Before transform, data is : </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">one_data</span><span class="p">))</span>

        <span class="n">f</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">transfer_one_instance</span><span class="p">,</span>
                              <span class="n">col_maps</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">col_maps</span><span class="p">,</span>
                              <span class="n">ori_header</span><span class="o">=</span><span class="n">ori_header</span><span class="p">,</span>
                              <span class="n">transformed_header</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">header</span><span class="p">)</span>
        <span class="n">new_data</span> <span class="o">=</span> <span class="n">data_instances</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">set_schema</span><span class="p">(</span><span class="n">new_data</span><span class="p">)</span>

        <span class="n">one_data</span> <span class="o">=</span> <span class="n">new_data</span><span class="o">.</span><span class="n">first</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">features</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;transfered data is : </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">one_data</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">new_data</span></div>

    <span class="k">def</span> <span class="nf">_transform_schema</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="n">header</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;[Result][OneHotEncoder]Before one-hot, data_instances schema is : </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">header</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">value_map</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">col_maps</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">col_idx</span> <span class="o">=</span> <span class="n">header</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span>
            <span class="n">new_headers</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">value_map</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
            <span class="n">new_headers</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">new_headers</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">col_idx</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">header</span> <span class="o">=</span> <span class="n">new_headers</span> <span class="o">+</span> <span class="n">header</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">header</span> <span class="o">=</span> <span class="n">header</span><span class="p">[:</span><span class="n">col_idx</span><span class="p">]</span> <span class="o">+</span> <span class="n">new_headers</span> <span class="o">+</span> <span class="n">header</span><span class="p">[</span><span class="n">col_idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">=</span> <span class="n">header</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;[Result][OneHotEncoder]After one-hot, data_instances schema is : </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">header</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">_init_cols</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>
        <span class="n">header</span> <span class="o">=</span> <span class="n">get_header</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">schema</span> <span class="o">=</span> <span class="n">data_instances</span><span class="o">.</span><span class="n">schema</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">=</span> <span class="n">header</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_index</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">header</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cols</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_index</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">idx</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;In binning module, selected index: </span><span class="si">{}</span><span class="s2"> is not integer&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="p">))</span>

                <span class="k">if</span> <span class="n">idx</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="n">header</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;In binning module, selected index: </span><span class="si">{}</span><span class="s2"> exceed length of data dimension&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="p">))</span>
                <span class="n">cols</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">header</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">cols</span>

<div class="viewcode-block" id="OneHotEncoder.record_new_header"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.one_hot_encoder.OneHotEncoder.record_new_header">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">record_new_header</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">cols</span><span class="p">,</span> <span class="n">header</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generate a new schema based on data value. Each new value will generate a new header.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        col_maps: a dict in which keys are original header, values are dicts. The dicts in value</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">col_maps</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_name</span> <span class="ow">in</span> <span class="n">cols</span><span class="p">:</span>
            <span class="n">col_maps</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">instance</span> <span class="ow">in</span> <span class="n">data</span><span class="p">:</span>
            <span class="n">feature</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">features</span>
            <span class="k">for</span> <span class="n">col_name</span> <span class="ow">in</span> <span class="n">cols</span><span class="p">:</span>
                <span class="n">this_col_map</span> <span class="o">=</span> <span class="n">col_maps</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span>
                <span class="n">col_index</span> <span class="o">=</span> <span class="n">header</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span>
                <span class="n">feature_value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">feature</span><span class="p">[</span><span class="n">col_index</span><span class="p">])</span>
                <span class="c1"># feature_value = str(feature_value)</span>
                <span class="k">if</span> <span class="n">feature_value</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">this_col_map</span><span class="p">:</span>
                    <span class="n">new_feature_header</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">feature_value</span><span class="p">))</span>
                    <span class="n">this_col_map</span><span class="p">[</span><span class="n">feature_value</span><span class="p">]</span> <span class="o">=</span> <span class="n">new_feature_header</span>

        <span class="k">return</span> <span class="n">col_maps</span></div>

<div class="viewcode-block" id="OneHotEncoder.merge_col_maps"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.one_hot_encoder.OneHotEncoder.merge_col_maps">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">merge_col_maps</span><span class="p">(</span><span class="n">col_map1</span><span class="p">,</span> <span class="n">col_map2</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">col_map1</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">col_map2</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="n">col_map1</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">col_map2</span>

        <span class="k">if</span> <span class="n">col_map2</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">col_map1</span>

        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">value_dict</span> <span class="ow">in</span> <span class="n">col_map2</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">col_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">col_map1</span><span class="p">:</span>
                <span class="n">col_map1</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value_dict</span>
                <span class="k">continue</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">col_1_value_dict</span> <span class="o">=</span> <span class="n">col_map1</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">value</span><span class="p">,</span> <span class="n">header</span> <span class="ow">in</span> <span class="n">value_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">value</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">col_1_value_dict</span><span class="p">:</span>
                        <span class="n">col_1_value_dict</span><span class="p">[</span><span class="n">value</span><span class="p">]</span> <span class="o">=</span> <span class="n">header</span>

        <span class="k">return</span> <span class="n">col_map1</span></div>

<div class="viewcode-block" id="OneHotEncoder.transfer_one_instance"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.one_hot_encoder.OneHotEncoder.transfer_one_instance">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">transfer_one_instance</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">col_maps</span><span class="p">,</span> <span class="n">ori_header</span><span class="p">,</span> <span class="n">transformed_header</span><span class="p">):</span>
        <span class="n">feature</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">features</span>
        <span class="n">feature_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">col_name</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">ori_header</span><span class="p">):</span>
            <span class="n">feature_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">feature</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">col_name</span> <span class="ow">in</span> <span class="n">transformed_header</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">col_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">feature_dict</span><span class="p">:</span>
                <span class="n">feature_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">value_dict</span> <span class="ow">in</span> <span class="n">col_maps</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">feature_value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">feature_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">col_name</span><span class="p">))</span>
            <span class="n">header_name</span> <span class="o">=</span> <span class="n">value_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">feature_value</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">header_name</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="n">feature_dict</span><span class="p">[</span><span class="n">header_name</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>

        <span class="n">feature_array</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">col_name</span> <span class="ow">in</span> <span class="n">transformed_header</span><span class="p">:</span>
            <span class="n">feature_array</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feature_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">])</span>

        <span class="n">feature_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">feature_array</span><span class="p">)</span>
        <span class="n">instance</span><span class="o">.</span><span class="n">features</span> <span class="o">=</span> <span class="n">feature_array</span>
        <span class="k">return</span> <span class="n">instance</span></div>

<div class="viewcode-block" id="OneHotEncoder.set_schema"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.one_hot_encoder.OneHotEncoder.set_schema">[docs]</a>    <span class="k">def</span> <span class="nf">set_schema</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instance</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">schema</span><span class="p">[</span><span class="s1">&#39;header&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">header</span>
        <span class="n">data_instance</span><span class="o">.</span><span class="n">schema</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">schema</span></div>

    <span class="k">def</span> <span class="nf">_get_meta</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">meta_protobuf_obj</span> <span class="o">=</span> <span class="n">onehot_meta_pb2</span><span class="o">.</span><span class="n">OneHotMeta</span><span class="p">(</span><span class="n">cols</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">meta_protobuf_obj</span>

    <span class="k">def</span> <span class="nf">_get_param</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">pb_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;in save model, col_maps: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">col_maps</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">value_dict</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">col_maps</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">values</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">value_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
            <span class="n">values</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
            <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;In _get_param, values: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">values</span><span class="p">))</span>
            <span class="n">data_type</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="n">encoded_variables</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">values</span><span class="p">:</span>
                <span class="n">encoded_variables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value_dict</span><span class="p">[</span><span class="n">v</span><span class="p">])</span>
            <span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">values</span><span class="p">]</span>

            <span class="n">value_dict_obj</span> <span class="o">=</span> <span class="n">onehot_param_pb2</span><span class="o">.</span><span class="n">ColsMap</span><span class="p">(</span><span class="n">values</span><span class="o">=</span><span class="n">values</span><span class="p">,</span>
                                                      <span class="n">encoded_variables</span><span class="o">=</span><span class="n">encoded_variables</span><span class="p">,</span>
                                                      <span class="n">data_type</span><span class="o">=</span><span class="n">data_type</span><span class="p">)</span>
            <span class="n">pb_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value_dict_obj</span>

        <span class="n">result_obj</span> <span class="o">=</span> <span class="n">onehot_param_pb2</span><span class="o">.</span><span class="n">OneHotParam</span><span class="p">(</span><span class="n">col_map</span><span class="o">=</span><span class="n">pb_dict</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result_obj</span>

<div class="viewcode-block" id="OneHotEncoder.export_model"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.one_hot_encoder.OneHotEncoder.export_model">[docs]</a>    <span class="k">def</span> <span class="nf">export_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_output</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">LOGGER</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Model output is : </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model_output</span><span class="p">))</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_output</span>

        <span class="n">meta_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_meta</span><span class="p">()</span>
        <span class="n">param_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_param</span><span class="p">()</span>
        <span class="n">result</span> <span class="o">=</span> <span class="p">{</span>
            <span class="n">MODEL_META_NAME</span><span class="p">:</span> <span class="n">meta_obj</span><span class="p">,</span>
            <span class="n">MODEL_PARAM_NAME</span><span class="p">:</span> <span class="n">param_obj</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">result</span></div>

    <span class="k">def</span> <span class="nf">_load_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_dict</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parse_need_run</span><span class="p">(</span><span class="n">model_dict</span><span class="p">,</span> <span class="n">MODEL_META_NAME</span><span class="p">)</span>
        <span class="n">model_param</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">model_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;model&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">())[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">MODEL_PARAM_NAME</span><span class="p">)</span>
        <span class="n">model_meta</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">model_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;model&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">())[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">MODEL_META_NAME</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">model_output</span> <span class="o">=</span> <span class="p">{</span>
            <span class="n">MODEL_META_NAME</span><span class="p">:</span> <span class="n">model_meta</span><span class="p">,</span>
            <span class="n">MODEL_PARAM_NAME</span><span class="p">:</span> <span class="n">model_param</span>
        <span class="p">}</span>

        <span class="n">col_maps</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">model_param</span><span class="o">.</span><span class="n">col_map</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">col_maps</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">col_maps</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">values</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">values</span>
            <span class="n">encoded_variables</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">encoded_variables</span>
            <span class="n">data_type</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">data_type</span>
            <span class="k">if</span> <span class="n">data_type</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;int&#39;</span><span class="p">,</span> <span class="s1">&#39;int64&#39;</span><span class="p">,</span> <span class="s1">&#39;int32&#39;</span><span class="p">]:</span>
                <span class="n">values</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
                <span class="n">values</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">values</span><span class="p">))</span>
            <span class="n">one_feature_col_map</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">encoded_variables</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">col_maps</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">one_feature_col_map</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>